package com.zoweunion.mechanic.controller;

import java.io.File;
import java.io.IOException;
import java.util.*;

import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zoweunion.mechanic.entity.TSBdCarDevRelationEntity;
import com.zoweunion.mechanic.service.ITSBdCarDevRelationService;
import com.zoweunion.mechanic.entity.vo.TSBdCarDevRelationVO;
import com.zoweunion.mechanic.entity.dto.TSBdCarDevRelationDTO;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * ${comments}
 *
 * @author wxx
 * @date 2023-05-25
 */
@RestController
@RequestMapping(path = "/tSBdCarDevRelation", produces = "application/json;charset=UTF-8")
// @Api(value = "/tSBdCarDevRelation", description = "${comments}", produces = "application/json;charset=UTF-8")

public class TSBdCarDevRelationController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(TSBdCarDevRelationController.class);

    @Autowired
    private ITSBdCarDevRelationService tSBdCarDevRelationService;

    @GetMapping("/listPage")
//     @ApiOperation(value = "条件查询列表分页", notes = "条件查询列表分页")
    public ResponseBean listPage(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {

        params.put("user_id", getCurrentUser(request).getId());
        PageInfo info = tSBdCarDevRelationService.listPage(params);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }


    /**
     * 新增
     */
    @PostMapping
//    @ApiOperation(value = "新增", notes = "")
    public ResponseBean create(@RequestBody @Validated TSBdCarDevRelationEntity tSBdCarDevRelation, HttpServletRequest request) {

        try {
            tSBdCarDevRelationService.save(tSBdCarDevRelation);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } catch (Exception e) {
            return new ResponseBean(Constants.CODE_ERROR, e.getMessage(), "");
        }


    }


    /**
     * 更新
     */
    @PutMapping
//    @ApiOperation(value = "更新", notes = "")
    public ResponseBean update(@RequestBody TSBdCarDevRelationEntity tSBdCarDevRelation, HttpServletRequest request) {

        if (tSBdCarDevRelation.getId() != null) {
            tSBdCarDevRelationService.updateById(tSBdCarDevRelation);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } else {
            return new ResponseBean(Constants.CODE_ERROR, "id必填", "");
        }
    }

    /**
     * 批量更新 (根据ids批量更新数据,更新字段都是相同的值)
     */
    @PutMapping("/batchUpdate")
//    @ApiOperation(value = "批量更新", notes = "")
    public ResponseBean batchUpdate(@RequestBody TSBdCarDevRelationDTO tSBdCarDevRelation, HttpServletRequest request) {

        if (tSBdCarDevRelation.getIds() != null) {

            List<TSBdCarDevRelationEntity> list = new ArrayList<>();
            for (String id : tSBdCarDevRelation.getIds()) {
                TSBdCarDevRelationEntity item = new TSBdCarDevRelationEntity();
                // 复制共同属性
                BeanUtils.copyProperties(tSBdCarDevRelation, item);
                // 根据id刷新
                item.setId(id);
                list.add(item);
            }

            tSBdCarDevRelationService.updateBatchById(list);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } else {
            return new ResponseBean(Constants.CODE_ERROR, "ids必填", "");
        }
    }


    /**
     * 详情
     */
    @GetMapping("/{id}")
//    @ApiOperation(value = "详情", notes = "")
    public ResponseBean detail(@PathVariable("id") Integer id) {
        try {

            TSBdCarDevRelationEntity tSBdCarDevRelation = tSBdCarDevRelationService.getById(id);
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", tSBdCarDevRelation);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseBean(Constants.CODE_ERROR, "操作失败", "");
        }

    }


    /**
     * 删除
     */
    @DeleteMapping
//    @ApiOperation(value = "删除", notes = "")
    public ResponseBean delete(@RequestBody Long[] ids) {
        try {

            tSBdCarDevRelationService.removeByIds(Arrays.asList(ids));
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new ResponseBean(Constants.CODE_ERROR, "操作成功", "");
        }
    }

    /**
     * 列表导出
     */
//    @GetMapping("/listExport")
//    public void listExport(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
//        List<TSBdCarDevRelationExcel> info = tSBdCarDevRelationService.listExport(params);
//        ExcelUtils.exportExcelToTarget(response, "列表导出", info, TSBdCarDevRelationExcel.class);
//    }


}
